package cn.tedu.qxhouserent.passport.controller;


import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.commons.web.JsonResult;
import cn.tedu.qxhouserent.passport.pojo.param.PermissionAddNewParam;
import cn.tedu.qxhouserent.passport.pojo.param.PermissionUpdateInfoParam;
import cn.tedu.qxhouserent.passport.pojo.vo.PermissionListItemVO;
import cn.tedu.qxhouserent.passport.pojo.vo.PermissionStandardVO;
import cn.tedu.qxhouserent.passport.service.PermissionService;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("permission")
@Slf4j
@Api(tags = "03.权限管理模块")
@Validated
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @PostMapping("add-new")
    @ApiOperation("添加权限")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    public JsonResult addNew(@Valid PermissionAddNewParam permissionAddNewParam){

        log.debug("开始处理添加权限业务");

        permissionService.addNew(permissionAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("根据ID删除权限")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(value = "权限ID", name = "id", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult delete(@Range(min = 1,message = "根据ID删除权限失败，请提交合法的ID值！")
                                 @RequestParam Long id) {

        permissionService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改权限")
    @ApiOperationSupport(order =300)
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的权限ID值！") Long id,
                                     @Valid PermissionUpdateInfoParam permissionUpdateInfoParam){
        log.debug("开始处理【修改权限详情】的请求，ID：{}，新数据：{}", id, permissionUpdateInfoParam);
        permissionService.updateInfoById(id, permissionUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询权限详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult getStandardById(@RequestParam @Range(min = 1, message = "请提交有效的权限ID值！")Long id){
        log.debug("开始处理【根据ID查询权限详情】的请求，参数：{}", id);
        PermissionStandardVO queryResult = permissionService.getStandardById(id);

        return JsonResult.ok(queryResult);
    }

    @GetMapping("list")
    @ApiOperation("查询权限列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query"),
            @ApiImplicitParam(name = "query", value = "查询类型", paramType = "query", dataType = "string")
    })
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult list (@Range(min = 1,message = "查询权限列表失败，请提供正确的页码值！") Integer page,String query){
        log.debug("开始处理【查询权限列表】的请求，页码：{}", page);
        if (page == null || page < 1){
            page = 1;
        }
        PageData<PermissionListItemVO> pageData = null;
        if ("all".equals(query)) {
            pageData = permissionService.list(page, Integer.MAX_VALUE);
        } else {
            pageData = permissionService.list(page);
        }
        return JsonResult.ok(pageData);
    }
}
